package com.shujia.wyh.day18;

import java.io.*;

/*
    复制文件夹D:\aaa_java下面所有文件和子文件夹内容到E:\aaa_java
 */
public class Test1 {
    public static void main(String[] args) {
        //将D:\aaa_java封装File对象
        File file = new File("D:\\aaa_java");

        copyFile(file);

    }

    public static void copyFile(File file){
        //如果这个file是一个文件夹，且不为空
        if(file!=null && file.isDirectory()){
            //获取原本的路径，修改D盘为E盘
            String pathName = file.getAbsolutePath().replace('D', 'E');
            //封装成File对象
            File file2 = new File(pathName);
            //创建这个文件夹
            boolean flag1 = file2.mkdir();
            //继续获取当前文件夹下的所有文件对象
            File[] files = file.listFiles();
            if(files!=null){
                for (File file1 : files) {
                    copyFile(file1);
                }
            }
        }else {
            //如果是一个文件
            if(file!=null){
                String oldPathName = file.getAbsolutePath();
                String newPathName = oldPathName.replace('D', 'E');

                File file1 = new File(newPathName);
                try {
                    boolean newFile = file1.createNewFile();
                    //读取旧文件中的内容，写入到新文件中
                    BufferedReader br = new BufferedReader(new FileReader(oldPathName));
                    BufferedWriter bw = new BufferedWriter(new FileWriter(newPathName));
                    String line = null;
                    while ((line=br.readLine())!=null){
                        bw.write(line);
                        bw.newLine();
                        bw.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
